package com.chookss.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.home.entity.StudyPlanDayEntity;
import com.chookss.tools.Utils;
import com.chookss.view.listener.StudyDialogListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;


public class StudyPlanYearDialog extends Dialog {
    private List<List<StudyPlanDayEntity>> dayLists = new ArrayList<>();
    private Context context;
    private StudyDialogListener listener = null;
    private TextView  tvCancel, tvSure;
    private RecyclerView rvView;
    private MyStudyDayAdapter adapter;
    private int position = 0;
    private String currentDate, choiceDate,year;

    public StudyPlanYearDialog(@NonNull Context context, List<List<StudyPlanDayEntity>> dayLists,String year,StudyDialogListener listener) {
        super(context);
        this.context = context;
        this.dayLists = dayLists;
        this.year = year;
        this.listener = listener;
        initLayoutRes();
    }

    public StudyPlanYearDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initLayoutRes();
    }

//    public void setSelect(String date) {
//        adapter.setSelect(position);
//    }

    private void initLayoutRes() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Date date = new Date(System.currentTimeMillis());
        currentDate = simpleDateFormat.format(date);
        choiceDate = simpleDateFormat.format(date);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_study_plan_year, null);
        rvView = view.findViewById(R.id.rvView);
        tvCancel = view.findViewById(R.id.tvCancel);
        tvSure = view.findViewById(R.id.tvSure);
        adapter = new MyStudyDayAdapter(R.layout.item_study_plan_day_dialog, context, dayLists.get(0));
        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(context, 6);
        rvView.setLayoutManager(gridLayoutManager1);
        rvView.setAdapter(adapter);

        tvCancel.setOnClickListener(v -> dismiss());
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onStudyClick(choiceDate);
                }
                dismiss();
            }
        });
        setContentView(view);
        initBottom();
    }

    protected void initBottom() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        Window wm = getWindow();
        lp.width =Utils.getScreenWidthPixel(context)-Utils.dip2px(context,24);
        lp.height = lp.WRAP_CONTENT;
        lp.dimAmount = 0.6f;
        wm.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        wm.setGravity(Gravity.CENTER);
        wm.setWindowAnimations(R.style.DialogFullWidthDimNotEnabled);
        getWindow().setAttributes(lp);
    }

    protected void safeDismiss() {
        if (isShowing()) {
            dismiss();
        }
    }
    class MyStudyDayAdapter extends BaseQuickAdapter<StudyPlanDayEntity, BaseViewHolder> {
        private Context context;

        public MyStudyDayAdapter(int layoutResId, Context context, @Nullable List<StudyPlanDayEntity> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolder helper, StudyPlanDayEntity item) {
            TextView tvTime = helper.getView(R.id.tvTime);
            tvTime.setText(item.getCreateTime());
            if ("1".equals(item.getIfComplete())) {
                //完成
                tvTime.setTextColor(Color.parseColor("#333333"));
                tvTime.setBackgroundResource(R.drawable.shape_circle_eaeaea);
            } else if ("0".equals(item.getIfComplete())) {
                //未完成
                tvTime.setTextColor(Color.parseColor("#08D2A5"));
                tvTime.setBackgroundResource(R.drawable.shape_circle_08d2a5);
            }
            int timeCompare = Utils.getMonthCompareSize(currentDate,year + "-" + item.getCreateTime());
            if (3 == timeCompare) {
                //还未开始
                tvTime.setTextColor(Color.parseColor("#333333"));
                tvTime.setBackgroundResource(R.drawable.shape_circle_white);
            }
            if (choiceDate.equals(year+ "-" + item.getCreateTime())) {
                //选中的
                tvTime.setTextColor(Color.parseColor("#ffffff"));
                tvTime.setBackgroundResource(R.drawable.shape_circle_main_color);
            }
            tvTime.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    choiceDate = year + "-" + item.getCreateTime();
                    notifyDataSetChanged();
                }
            });
        }
    }

}
